package homework;

public class Tongbu {
    static int j = 0;
    //1.设计4个线程，其中两个线程每次对j增加1，另外两个线程对j每次减少1。
    //要求：使用内部类实现线程，对j增减的时候不考虑顺序问题。
    public static void main(String[] args) {
        homework.MyTask t = new homework.MyTask(j);
        homework.MyTask2 t1 = new homework.MyTask2(j);

        Thread t0 = new Thread(t);
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t1);
        Thread t4 = new Thread(t1);

        t0.setName("线程1");
        t2.setName("线程2");
        t3.setName("线程3");
        t4.setName("线程4");
        t0.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class MyTask implements Runnable{
    private int j;
    public MyTask(int j){
        this.j = j;
    }
    @Override
    public void run() {
        j++;
        System.out.println(Thread.currentThread().getName() + "正在执行，j的值为：" + j);
    }
}
class MyTask2 implements Runnable{
    private int j;
    public MyTask2(int j){
        this.j = j;
    }
    @Override
    public void run() {
        j--;
        System.out.println(Thread.currentThread().getName() + "正在执行，j的值为：" + j);
    }
}

